$(function(){
    var cityname = GetQueryString('cutomecity');
    if(cityname){
        data = {cityname:cityname,start:"1",end:"12",overstart:"1",overend:"12",startrate:"0",endrate:"100",overstartrate:'0',overendrate:"100",typeurl:"addurl"};
        ajaxcityrisk(data);
    }
    ajax({
			'url':'/admin/Singlerate/region',
			'type':'post',
			'data':{},
			'dataType':'json',
			'success':function(response) {
				if(response.success=='OK'){
					var data = response.data;
					var data_sheng = [];
					var sheng = '#sheng';
					var shi = '#shi';
					for (var i = 0; i < data.length; i++) {
						$(sheng).append($('<option value="'+data[i].id+'">'+data[i].name+'</option>'));
						data_sheng[data[i].id] = data[i];
					}
					$(sheng).on('change',function(){
						var thisid = $(this).val();
						if(thisid == "" || thisid == null || thisid == undefined){
							$(shi).find('option').remove();
							$(shi).append($('<option value="0" disabled="disabled" selected="selected">请选择</option>'));
							return false;

						}
						var shi_ = data_sheng[thisid].shi;
						var provinceid_shi = $(shi).attr('provinceid');
						if(provinceid_shi==thisid){
							return false;
						}
						$(shi).attr('provinceid',thisid);
						$(shi).empty();
						$(shi).append($('<option value="0" disabled="disabled" selected="selected">请选择</option>'));
						for (var i = 0; i < shi_.length; i++) {
							$(shi).append($('<option value="'+shi_[i].id+'">'+shi_[i].name+'</option>'));
						}
					});
				}
			}
	})

});

function ajaxcityrisk(data){
	ajax({
		'url':'/admin/Singlerate/cityriskkong',
		'type':'post',
		'data':data,
		'dataType':'json',
		'success':function(response) {
            if(response.resdata && response.resdata.length!=0){
				setItem('orders', response.resdata);
	            var totalPage = Math.ceil(response.resdata.length / 15);
	            showPage(1, totalPage, addInfo);recovery();
	            addInfo(1, totalPage); 
			}else{
                addNotify();
            }
			
			// addInfo(response.resdata);
		}
	},true)
}

/**
 * 城市风控明细
 */
$(".citytyperisk").click(function(){
	var cityname = $("input[name='cutomecity']").val();
	if(!cityname){
		sweetAlert('','城市名称不能为空','error');
        return false;
	}
	data = {cityname:cityname,start:"1",end:"12",overstart:"1",overend:"12",startrate:"1",endrate:"100",overstartrate:'1',overendrate:"100"};
	ajaxcityrisk(data);
});
$(".cutomecityrisk").click(function(){
	var sheng = $("#sheng").val();//省
	var shi = $("#shi").val();//是
	var tooend = $("#tooend").val();//开始过单时间选择
	var toostart = $("#toostart").val();//结束过单时间选择
	var overstart = $("#overstart").val();//结束逾期选择
	var overend    = $("#overend").val();//开始逾期全部时间
	var overstartrate   = $("#overstartrate").val();//开始逾期百分比
	var overendrate   = $("#overendrate").val();//结束逾期百分比
	var startrate   = $(".startrate").val();//开始过单百分比
	var endrate   = $(".endrate").val();//结束过单百分比
	var signrate1  = $(".signrate1").val();//标红开始过单百分比
	var signendrate1   = $(".signendrate1").val();//标红结束过单百分比
	var signrate2  = $(".signrate2").val();//标红开始过单百分比
	var signendrate2   = $(".signendrate2").val();//标红结束过单百分比
    var signeradio  = $("input[name='0']:checked").val();
    var signeradio1  = $("input[name='1']:checked").val();
	if(signeradio == "1"){
        if(!isIntNum(overstart) || !isIntNum(overend) || overstart == "" || overend == ""){
            sweetAlert('', '逾期结束月不能为空或请输入数字', 'error');
            return false;
        }
        if(parseInt(overstart) > parseInt(overend)){
            sweetAlert('','逾期起始月不能大于结束月','error');
            return false; 
        }
        if(overstart == 0 || overend == 0){
            sweetAlert('','逾期起始月不能输入零','error');
            return false; 
        }
        if(!isIntNum(overstart) || !isIntNum(overend)){
            sweetAlert('','查询逾期条件请输入数字','error');
            return false;
        }
        
    }else{
        var selectvale = $(".select_time").val();
        if(selectvale != 0){
            overstart  = selectvale;
            overend    = selectvale;
        }else if(selectvale == 0){
                overstart = 1;
                overend   = 12;
        }
    }
    if(signeradio1 == "3"){ 
        if(!isIntNum(toostart) || !isIntNum(tooend) || toostart == "" || tooend == ""){
            sweetAlert('', '过单结束月不能为空或请输入数字', 'error');
            return false;
        }
        if(parseInt(toostart) > parseInt(tooend)){
            sweetAlert('','过单起始月不能大于结束月或请输入数字','error');
            return false; 
        }
        if(toostart == 0 || tooend == 0){
            sweetAlert('','过单起始月不能输入零','error');
            return false; 
        }
        if(!isIntNum(toostart) || !isIntNum(tooend)){
            sweetAlert('','查询过单条件请输入数字','error');
            return false;
        }
        
    }else{
        var selecttoo = $(".select_timeend").val();
        if(selecttoo != 0){
            toostart  = selecttoo;
            tooend    = selecttoo;
        }else if(selecttoo == 0){
                toostart = 1;
                tooend   = 12;
        }
    }

	if(startrate == "" || endrate == ""){
		startrate = 0;
		endrate = 100;
	}else if(parseInt(startrate) > parseInt(endrate)){
        sweetAlert('','起始值不能大于结束值或请输入数字','error');
        return false;
    }
    if(!isIntNum(startrate) || !isIntNum(endrate)){
        sweetAlert('', '请输入数字', 'error');
        return;
    }
	if(overstartrate == "" || overendrate == ""){
		overstartrate = 0;
		overendrate = 100;
	}else if(parseInt(overstartrate) > parseInt(overendrate)){
        sweetAlert('','起始值不能大于结束值或请输入数字','error');
        return false;
    }
    if(!isIntNum(overstartrate) || !isIntNum(overendrate)){
        sweetAlert('','请输入数字','error');
        return false;
    }
	data = {
			typelv:"toorate",
			start:toostart,
			end:tooend,
			provinceid:sheng,
		   	cityid:shi,
			overstart:overstart,
			overend:overend,
			overstartrate:overstartrate,
			overendrate:overendrate,
			startrate:startrate,
			endrate:endrate,
			overstartcolor1:signrate1,
			overendcolor1:signendrate1,
			overstartcolor2:signrate2,
			overendcolor2:signendrate2,
		}
	ajaxcityrisk(data)
});
/**
 * 风控明细
 * @param {[type]} data [description]
 */
function addInfo(page,totalPage) {
		var data = pageData(page,totalPage)
        if (data.length <= 0) {
            $('.table_hover tr').remove();
            return;
        }
        showPage(page,totalPage, addInfo);
        $('.table_hover tr').remove();
        $('.tr_item_title').children().remove();
        var children = $('.tr_checkbox').children();
        for (var i = 0; i < children.length; i++) {
            children[i].remove();
        }
        var th = '';
        var tr = '';
        //var months = ['当月', '上月', '第3月', '第4月', '第5月', '第6月', '第7月', '第8月', '第9月', '第10月', '第11月', '第12月'];
        tr += "<th>序号</th> <th>城市名称</th> <th>所属省份</th> <th>GW在职总数</th> <th>城市渠道激活数</th><th>CS姓名</th> ";
        tr += "<th>CS岗位号</th> <th>SZ姓名</th>  <th>SZ岗位号</th> <th>总过单数</th> <th>总过单比</th> <th>总过单率</th>";
        th += "<th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th>";
        th += "<th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th><th> <input type='checkbox' class='checkbox_input'> </th>";
        var trStatus = true;
        for (var i = 0; i < data.length; i++) {
        	cityname = "";
        	citygwhn = "";
           	
            var str = '';
            str += "<td>" + (i + 1) + "</td>";
            str += "<td>" + data[i].cityname+ "</td>";
            str += "<td>" + data[i].provanem + "</td>";
            str += "<td>" + data[i].citycount + "</td>";
            str += "<td>" + data[i].waycount + "</td>";
            str += "<td>" + data[i].name + "</td>";
            str += "<td>" + data[i].usernum + "</td>";
            str += "<td>" + data[i].szname + "</td>";
            str += "<td>" + data[i].szusernum + "</td>";
            
            var ordertoores = data[i].tooarr;
            if(ordertoores){
            		for (var j = 0; j < ordertoores.length; j++) {
		            	if(ordertoores[j].tooorder || ordertoores[j].tooorderccount || ordertoores[j].tooordercount){
		            		str += "<td>" + ordertoores[j].tooorder + "</td>";
		                    str += "<td>" + ordertoores[j].tooordercount + "</td>";
		                    str += "<td>" + ordertoores[j].tooorderccount + "%</td>";
		            	}
		            }
		            for (var j = 0; j < ordertoores.length; j++) {
		            	if(ordertoores[j].ordercoun || ordertoores[j].ordermon || ordertoores[j].orderrate){
		            		str += "<td>" + ordertoores[j].ordercoun + "</td>";
		                    str += "<td>" + ordertoores[j].ordermon + "</td>";
		                    str += "<td>" + ordertoores[j].orderrate + "%</td>";
		            	}
		            }
		            
            }
            if(!ordertoores){
				str += "<td>" + "暂无数据" + "</td>";
                str += "<td>" + "暂无数据" + "</td>";
                str += "<td>" + "暂无数据" + "</td>";
                for (var j = data[i].start; j <= data[i].end; j++) {
            		str += "<td>" + "暂无数据" + "</td>";
		            str += "<td>" + "暂无数据" + "</td>";
		            str += "<td>" + "暂无数据" + "</td>";
            	}
        	}
            if(trStatus){
            	
            	for (var j = data[i].start; j <= data[i].end; j++) {
            		
            		if(j == 1){
            			months = "当月";
            		}else if(j == 2){
            			months = "上月";
            		}else{
            			months = "第"+j+"月";	
            		}
                    tr += "<th>" + months + "过单数</th>";
                    tr += "<th>" + months + "过单比</th>";
                    tr += "<th>" + months + "过单率</th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
            	}
            
            	tr += "<th>总逾期数</th> <th>总逾期比</th> <th>总逾期率</th>";
	            th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
	            th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
	            th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
            }

            var orders     = data[i].orderarr;
            if(orders){
            	for (var j = 0; j < orders.length; j++) {
	            	if(orders[j].overduecount || orders[j].overduecountorder || orders[j].overduecountrate){
	            		str += "<td>" + orders[j].overduecount + "</td>";
	                    str += "<td>" + orders[j].overduecountorder + "</td>";
	                    str += "<td>" + orders[j].overduecountrate + "%</td>";
	            	}
	            }
	            for (var j = 0; j < orders.length; j++) {
	            	if(orders[j].overdue != undefined || orders[j].overdueorder != undefined  || orders[j].overduerate != undefined ){
	            		str += "<td>" + orders[j].overdue + "</td>";
	                    str += "<td>" + orders[j].overdueorder + "</td>";
	                    str += "<td>" + orders[j].overduerate + "%</td>";
	            	}
	            }
            }
            if(!orders){
				str += "<td>" + "暂无数据" + "</td>";
                str += "<td>" + "暂无数据" + "</td>";
                str += "<td>" + "暂无数据" + "</td>";
                for (var j = data[i].overstart; j <= data[i].overend; j++) {
            		str += "<td>" + "暂无数据" + "</td>";
		            str += "<td>" + "暂无数据" + "</td>";
		            str += "<td>" + "暂无数据" + "</td>";
            	}
        	}
            if(trStatus){
            	for (var j = data[i].overstart; j <= data[i].overend; j++) {
            		if(j == 1){
            			months = "当月";
            		}else if(j == 2){
            			months = "上月";
            		}else{
            			months = "第"+j+"月";	
            		}
                    tr += "<th>" + months + "逾期数</th>";
                    tr += "<th>" + months + "逾期比</th>";
                    tr += "<th>" + months + "逾期率</th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
                    th += "<th> <input type='checkbox' class='checkbox_input'> </th>";
            	}
            	th+='<th calss="btn_rate selected" id="btn_filter">'+
                      '<button type="button" class="btn btn-warning btn_filter" >过滤</button>'+
                      '</th>'+
                      '<th calss="btn_rate selected" id="btn_recovery">'+
                      '<button type="button" class="btn btn_recovery" >重置</button>'+
                      '</th>';
            }
            str += "<td></td><td></td>";
            var trcolor = "";
            if(data[i].typesign == "red"){
            	trcolor = "color:red";
            }
            var trStatus = false;
            var str2 = "<tr style="+trcolor+">" + str + "</tr>";
            $('.table_hover').append(str2);
        }
        tr+= "<th><th>";
        $('.tr_item_title').append(tr);
        $('.thead_item .tr_checkbox').append(th);
        if (!filterTag) {
        	tableHide();
        }else{
            recovery();
        }
}

function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}


$('.radio').click(function () {
        var value = $(this).val();
        switch(value) {
            case "0":
                $('#overstart').attr("disabled", "disabled");
                $('#overend').attr("disabled", "disabled");
                $("#overstart").val("");
                $("#overend").val("");
                $('.select_time').removeAttr("disabled");
                break;
            case "1":
                $('.select_time').attr("disabled", "disabled");
                $('#overstart').removeAttr("disabled");
                $('#overend').removeAttr("disabled");
                break;
        }
    });
$('.radios').click(function () {
        var value = $(this).val();
        switch(value) {
            case "2":
                $('.start').attr("disabled", "disabled");
                $('.end').attr("disabled", "disabled");
                $(".start").val("");
                $(".end").val("");
                $('.select_timeend').removeAttr("disabled");
                break;
            case "3":
                $('.select_timeend').attr("disabled", "disabled");
                $('.start').removeAttr("disabled");
                $('.end').removeAttr("disabled");
                break;
        }
    });

$('body').on('click', '.num', function()
{
    var value = parseInt($(this).attr('value'));
    var number = $('.end').attr('value');
    if (value == 0) return;
    if (value > number) return;
    var end = 10 * value;
    var start = end - 10 + 1;
    var result = pageData(start, end);
    addInfo(result);
    $('.prev').attr('value', value - 1);
    $('.next').attr('value', value + 1);
    $('.num').removeClass('a_active');
    var childrens = $('.black2').children();
    var num = $(this).attr('number');
    childrens.eq(num).addClass('a_active');
    $('.prev').attr('number', parseInt(num) - 1);
    $('.next').attr('number', parseInt(num) + 1);
});

$(".ExportExcelDet").click(function(){
	var status = $(this).val();
	if(!status){
		return false;
	}

	ajax({
		'url':'/admin/Singlerate/ExportExcelDetailed',
		'type':'post',
		'data':{status:status,arrarray:filterIndexs},
		'dataType':'json',
		'success':function(response) {
			if(response.success == "OK"){
                location.href = response.path;
			}
		}
	},true)
});

